<?php
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Document\Controller;
use \Admin\Controller\InitController;
define('MODULE_CACHE', DATA_PATH.'caches_model/');
class YardController extends InitController {
    public function _initialize() {
    	parent::_initialize();
        $this->categorys = getcache('category', 'commons');
        $this->modelid = 2;
        $this->models = getcache('model', 'commons');
        $this->userid = (int) cookie('_userid');
        $this->db = D('DamaLog');
        $this->account_db = D('DamaAccount');
        /* 将页码变量赋值到模板 */
    }

    /* 打码记录管理 */
    public function manage() {
        $page = I('page', '0', 'int');
        $count = $this->db->count();
        $infos = $this->db->order("status ASC, id DESC")->page($page, 10)->select();
        $accounts = array();
        if ($infos) {
            foreach ($infos as $key => $value) {
                $accounts[] = $value['account_id'];
            }
            $accounts = array_unique($accounts);
            $accounts_map = array();
            $accounts_map['id'] = array("IN", $accounts);
            $accounts_infos = $this->account_db->where($accounts_map)->getField('id, aid, name');
            $document_ids = array();
            foreach ($accounts_infos as $key => $value) {
                $document_ids[] = $value['aid'];
            }
            $model = $this->models[$this->modelid];
            $document_ids = array_unique($document_ids);
            $document_map = array();
            $document_map['id'] = array("IN", $document_ids);
            $document_infos = M($model['tablename'])->where($document_map)->getField('id, title');
            // $aids = array_unique($aids);
            // dump($document_infos);
        }
        $pages = page($count, 10);
        include $this->admin_tpl('yard_manage');
    }

    /* 导入excel记录 */
    public function import() {
        if (submitcheck('dosubmit', 'GP')) {
            $param = I('param.');
            $pagesize = 2;//每次导入10条记录
            $page = I('page', 1, 'intval');
            /* 获取文档信息 */
            $model = $this->models[$this->modelid];
            $system_info = M($model['tablename'])->getById($param['aid']);
            $file = $param['file'];
            $file = SITE_PATH.str_replace("./", "/", $file);
            if ($page == 1) {
                if (!file_exists($file)) {
                    $this->error('导入的目标文件不存在');
                }
                delcache('yard_import_data', 'tmp');
                import("Org.Util.PHPExcel");
                $PHPExcel=new \PHPExcel();
                import("Org.Util.PHPExcel.Reader.Excel5");
                $PHPReader=new \PHPExcel_Reader_Excel5();
                $PHPExcel=$PHPReader->load($file);
                $currentSheet=$PHPExcel->getSheet(0);
                $allColumn=$currentSheet->getHighestColumn();
                $allRow=$currentSheet->getHighestRow();
                $arr_tmp = $array = array();
                for($currentRow=1;$currentRow<=$allRow;$currentRow++){
                    //从哪列开始，A表示第一列
                    for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){
                        //数据坐标
                        $address=$currentColumn.$currentRow;
                        //读取到的数据，保存到数组$arr中
                        $arr_tmp[$currentRow][$currentColumn]=$currentSheet->getCell($address)->getValue();
                    }        
                }
                if(empty($arr_tmp)) $this->error('您导入的目标文件没有数据');
                foreach ($arr_tmp as $key => $value) {
                    if (stripos($value['A'], 'jsz') !== 0) continue;
                    $array[$value['A']] = (int) $value['B'];
                }
                if(empty($array)) $this->error('您导入的目标文件没有数据');
                setcache('yard_import_data', $array, 'tmp');
            }
            $array = getcache('yard_import_data', 'tmp');
            if (count($array) > $pagesize) {
                $i = 0;
                foreach ($array as $key => $value) {
                    $start = ($page - 1) * $pagesize;
                    $end = $start + $pagesize;
                    if($i >= $start && $i < $end) {
                        $import_arr[$key] = $value;
                    }
                    $i++;
                }
                unset($i);
            } else {
                $import_arr = $array;
            }
            if (count($import_arr) > 0) {
                foreach ($import_arr as $key => $value) {
                    $yard_map = array();
                    $yard_map['aid'] = $param['aid'];
                    $yard_map['name'] = $key;
                    $account_info = M('DamaAccount')->where($yard_map)->find();
                    if ($account_info['id'] > 0) {
                        $log_data = array();
                        $log_data['account_id'] = $account_info['id'];
                        $log_data['yard_num'] = $value;
                        $log_data['yard_u_coin'] = round($system_info['price'] * $value);
                        $log_data['status'] = 0;
                        $log_data['yard_time'] = strtotime($param['yard_time']);
                        $log_data['uid'] = $account_info['uid'];
                        $log_data['aid'] = $account_info['aid'];
                        D('DamaLog')->update($log_data);
                        $this->account_db->where($yard_map)->setField('updatetime',NOW_TIME);
                    }
                }
                $param['dosubmit'] = true;
                $param['page'] = $page+1;

                $this->success('导入成功，正在进入下一批...<br/><font class="red">请勿关闭或刷新浏览器</font>', U(ACTION_NAME, $param));           
            } else {
                delcache('yard_import_data', 'tmp');
                @unlink($file);//删除上传的临时文件
                $this->success('打码数据导入成功', U('manage', array('menuid' => 225)));
            }
        } else {
            $form = new \Common\Library\form();
            $dialog = true;
            include $this->admin_tpl('yard_import');
        }
        
    }

    /* 删除 */
    public function delete() {
        if (submitcheck('dosubmit')) {
            $ids = (array) $_POST['id'];
            if(empty($ids)) $this->error('请选择要删除的项目');
            $sqlmap = array();
            $sqlmap['id'] = array("IN", $ids);
            $this->db->where($sqlmap)->delete();
            $this->success('操作成功');
        } else {
            $this->error('请勿非法操作');
        }
    }
    /**
     * 删除打码记录
     */
    public function deletes(){
        $ids = I('id');
        $account = $this->db->where(array('id'=>$ids))->find();
        if(!$account){
            $this->error('请勿非法操作!');
        }
        $result = $this->db->where(array('id'=>$ids))->delete();
        if(!$result){
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }
}